<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/Abstract.php';

/**
 * Helper to generate an "url" (using for the href attribute)
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3url extends Zend_View_Helper_Abstract
{

	/**
	 * The TYPO3 plugin
	 *
	 * @var tslib_pibase
	 */
	private $_pi;

	/**
	 * Generates an URL to the current page (the caller page) using PI vars.
	 * This is the shortform of url()->thisPagePIvars().
	 *
	 * If no arguments are passed, returns a Tx_Zview_Helper_Link object.
	 *
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @param integer $altPageId
	 * @return string|Tx_Zview_Helper_T3url
	 */
	public function t3url(array $overrulePIvars=array(), $cache=true, $clearAnyway=false, $altPageId=0)
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->thisPagePIvars($overrulePIvars, $cache, $clearAnyway, $altPageId);
	}

	/**
	 * Generates an URL to a certain page.
	 *
	 * @param integer $id
	 * @param array $urlParameters
	 * @return string The generated URL
	 */
	public function toPage($id, array $urlParameters=array())
	{
		$target = '';
		return $this->_pi->pi_getPageLink($id, $target, $urlParameters);
	}

	/**
	 * Generates an URL to a certain page using PI vars.
	 *
	 * @param integer $id
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @return string The generated URL
	 */
	public function toPagePIvars($id, array $overrulePIvars=array(), $cache=true, $clearAnyway=false)
	{
		return $this->_pi->pi_linkTP_keepPIvars_url($overrulePIvars, $cache, $clearAnyway, $id);
	}

	/**
	 * Generates an URL to the current page (the caller page).
	 *
	 * @param array $urlParameters
	 * @return string The generated URL
	 */
	public function thisPage(array $urlParameters=array())
	{
		$id = $GLOBALS['TSFE']->page['uid'];
		$target = '';
		return $this->_pi->pi_getPageLink($id, $target, $urlParameters);
	}

	/**
	 * Generates an URL to the current page using PI vars (the caller page).
	 *
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @param integer $altPageId
	 * @return string The generated URL
	 */
	public function thisPagePIvars(array $overrulePIvars=array(), $cache=true, $clearAnyway=false, $altPageId=0)
	{
		return $this->_pi->pi_linkTP_keepPIvars_url($overrulePIvars, $cache, $clearAnyway, $altPageId);
	}

    /**
     * Set the View object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_View_Helper_Abstract
     */
	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;
		$this->_pi = $view->getPi();
		if(empty($this->_pi) || (!$this->_pi instanceof tslib_pibase)) {
			throw new Exception('Please set tslib_pibase using $view->setPi($this) in your plugin class.');
		}
        return $this;
	}

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3url.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3url.php']);
}
